package 课设;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;

public class usertable extends JFrame {
    private JTable table;
    private MyTableModel tablemodel;
    private JButton b1,b4;
    private JToolBar tool;

    public usertable() {
        this.setSize(800, 600);
        this.setTitle("userMessage");
        this.setLocationRelativeTo(getOwner());

        tablemodel = getModel();
        table = new JTable(tablemodel);
        table.setPreferredScrollableViewportSize(new Dimension(500, 300));
        JScrollPane scroll = new JScrollPane(table);
        getContentPane().add(scroll, BorderLayout.CENTER);

        b1 = new JButton("添加用户");
        b1.setToolTipText("添加");
        b1.setFocusable(false);
        b1.setHorizontalTextPosition(SwingConstants.CENTER);
        b1.setVerticalTextPosition(SwingConstants.BOTTOM);


        //创建工具栏，添加按钮
        tool = new JToolBar();
        tool.add(b1);

        //添加工具栏
        getContentPane().add(tool,BorderLayout.NORTH);
        b1.addActionListener(new ActionListener() {//增
            public void actionPerformed(ActionEvent e) {
                userAdd w = new userAdd("",1);
                w.setVisible(true);
                dispose();
            }
        });


        b4 = new JButton("查看");
        b4.setToolTipText("查询");
        b4.setFocusable(false);
        b4.setHorizontalTextPosition(SwingConstants.CENTER);
        b4.setVerticalTextPosition(SwingConstants.BOTTOM);

        tool.add(b4);

        b4.addActionListener(new ActionListener() {//
            public void actionPerformed(ActionEvent e) {
                db dbcon;
                try{
                    dbcon = new db();
                    if(table.getSelectedRows().length > 0) {
                        //获得JTable中选中行的序列
                        int[] selRowIndexs = table.getSelectedRows();
                        if(selRowIndexs != null) {
                            int userId = (int)table.getValueAt(selRowIndexs[0], 0);
                            dispose();
                            user_show a = new user_show(userId);
                            a.setVisible(true);
                        }
                    }else
                        JOptionPane.showMessageDialog(null,"未选中");
                }catch (Exception ex){
                    System.out.println(ex.getMessage());
                }
            }
        });
    }

    private MyTableModel getModel() {
        MyTableModel tableModel = new MyTableModel();
        db dbcon;
        try {
            dbcon = new db();
            ResultSet rs = dbcon.executeQuery("select * from users");

            ResultSetMetaData rsmd = rs.getMetaData();//获得查询结果中的列名，填充表格模型列

            int i;
            for (i = 1; i <= rsmd.getColumnCount(); i++)
                tableModel.addColumn(rsmd.getColumnName(i));

            ArrayList<User> v = new ArrayList<>();//获取查询结果中的元组，填充表格模型行
            while (rs.next()) {
                User user = new User();
                user.setUserId(rs.getInt("userId"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));

                v.add(user);
            }
            rs.close();
            //1
            for (i = 0; i < v.size(); i++) {
                tableModel.addRow(new Object[]{v.get(i).getUserId(), v.get(i).getUsername(), v.get(i).getPassword()});
            }
        } catch (SQLException sqle) {
            System.out.println(sqle);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return tableModel;
    }

    public static void main(String[] args) {
        usertable a = new usertable();
        a.setVisible(true);
    }
}
